package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.utils.CurrentUserHoler;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    //查询线索列表
    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    //
    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }

    //跟进线索
    @Transactional(rollbackFor = Exception.class)//事务注解,本来只能回滚运行异常,现在加上rollbackFor = Exception.class
    //可以回滚所有异常
    @Override
    public void trackClue(Clue clue) throws Exception {
        //根据ID跟新线索的基本信息
        clue.setStatus(3);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //添加跟进记录---插入一条数据
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//默认当前用户ID  todo
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());//跟进记录
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    //标记为伪线索
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void falseClue(Integer id, MarkFalseClueDto dto) throws Exception {
        //更新线索状态
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);//伪线索状态
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //添加跟进记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//默认当前用户ID
        clueTrackRecord.setType(0);//跟进类型:0:伪线索，1:接通，2:拒绝，3:无人接听
        clueTrackRecord.setFalseReason(dto.getReason());
        clueTrackRecord.setRecord(dto.getRemark());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    //线索转商机
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toBusiness(Integer id) {
        //根据ID更新线索状态
        Clue clue =clueMapper.selectById(id);
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //保存商机信息(根据线索信息拼装商机信息)copy 复制属性
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setUserId(null);
        business.setStatus(1);//待分配
        business.setClueId(id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.insert(business);
    }

    //线索池列表查询

    @Override
    public PageResult<Clue> listpoolClues(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listpoolClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(), page.getRecords());

    }
}
